﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TravelSite.DAL;
using TravelSite.DAL.Repositories;
using TravelSite.Entities;
using TravelSite.Code;
using System.Globalization;

namespace TravelSite.Admin
{
    public partial class ContactManagement : BaseDataPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCombo();
                LoadData();
            }
        }

        public override void LoadData()
        {
            using (var repo = RepositoryFactory.ResolveRepository<ContactRepository>())
            {
                dlAlphabets.DataSource = repo.GetAlphabet(true, "last", SelectedAlphabet);
                dlAlphabets.DataBind();

                dlContacts.DataSource = repo.Search(ddlAgentIdFilter.SelectedValue == "-1" ? null : (int?)Convert.ToInt32(ddlAgentIdFilter.SelectedValue), SelectedAlphabet, ddlFieldFilter.SelectedValue, txtValueFilter.Text);
                dlContacts.DataBind();
                
                upMain.Update();
            }
        }

        private void LoadCombo()
        {
            using (var repo = RepositoryFactory.ResolveRepository<UserRepository>())
            {
                ddlAgentIdFilter.DataSource = repo.AgentsCombo(true);
                ddlAgentIdFilter.DataBind();
            }

            using (var repo = RepositoryFactory.ResolveRepository<ContactRepository>())
            {
                ddlFieldFilter.DataSource = repo.ContactColumnList();
                ddlFieldFilter.DataBind();
            }
        }

        protected string GetPhone(string wphone, string mphone)
        {
            return (string.IsNullOrEmpty(wphone) ? "&nbsp;" : wphone.ToString() + " <span>Work</span>") + "<br/>" + (string.IsNullOrEmpty(mphone) ? "&nbsp;" : mphone.ToString() + " <span>Mobile</span>");
        }

        protected string GetCompanyAndJob(object companyData, object jobData)
        {
            string company = "";
            if (companyData != null)
                company = companyData.ToString();

            string job = "";
            if (jobData != null)
                job = jobData.ToString();

            return (string.IsNullOrEmpty(company) ? "&nbsp;" : company.ToString()) + "<br/>" + (string.IsNullOrEmpty(job) ? "&nbsp;" : job.ToString());
        }

        protected string GetPin(object pin)
        {
            string sPin = "";
            if (pin != null)
                sPin = pin.ToString();
            return (string.IsNullOrEmpty(sPin) ? "&nbsp;" : sPin + " <span>Pin</span>");
        }

        protected string SelectedAlphabet
        {
            set { ViewState["SelectedAlphabet"] = value; }
            get { return ViewState["SelectedAlphabet"] == null?"ALL": ViewState["SelectedAlphabet"].ToString(); }
        }

        protected string AlphabetCss(string item)
        {
            return item == SelectedAlphabet ? "alpha_link_selected" : "";
        }

        protected void Alphabet_Click(object sender, EventArgs e)
        {
            LinkButton lnkAlphabet = (LinkButton)sender;
            ViewState["SelectedAlphabet"] = lnkAlphabet.Text;
            LoadData();
        }

        protected void ddlAgentIdFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void dlContacts_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Contact item = (Contact)e.Item.DataItem;
                
                LinkButton lb = (LinkButton)e.Item.FindControl("lbDelete");
                lb.Visible = item.CanDelete;
            }
        }

        protected void Contact_Command(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());

            using (var repo = RepositoryFactory.ResolveRepository<ContactRepository>())
            {
                switch (e.CommandName)
                {
                    case "delete":
                        repo.Delete(id);
                        break;
                }
            }

            LoadData();
        }
    }
}